********************************************************************************
*
* Supplement to Table2_main.do: clinical outcomes regressions
*
* This script is called by Table2_main.do. 
*
********************************************************************************




****
**** settings 
****
* survival and readmission data 
local fpath_survreadm_dat = "/disk/agedisk4/medicare.work/sacarny-DUA51934/shruthi-dua51934/replication_files/survreadm/input"

* crosswalks
local fpath_crosswalks = "/disk/agedisk4/medicare.work/sacarny-DUA51934/shruthi-dua51934/replication_files/build/crosswalks"

* build folder 
local fpath_build_output = "/disk/agedisk4/medicare.work/sacarny-DUA51934/shruthi-dua51934/replication_files/build/output"

* output directory 
local fpath_output = "/disk/agedisk4/medicare.work/sacarny-DUA51934/shruthi-dua51934/replication_files/output_20230606/"


** read in the cohort-hospital-year level file with surv and readm outcomes 
insheet using "`fpath_survreadm_dat'/stackedv2_20230606.csv", clear


** drop immediately divested hospitals from the control group 
preserve 
import excel "`fpath_crosswalks'/Acq AHA Xwalk (Dalton).xlsx", sheet("combined (stata input)") firstrow clear
keep if (divested_yr <= 2008) | (divested_yr == 2015 & target2 == 1)
rename id_aha_dalton id
keep id 
tempfile drop_divested
save `drop_divested', replace
restore 

cap drop _merge 
merge m:1 id using `drop_divested', keep(master) nogen



* collapse to hosp-yeargroup-measure level
* the yeargroups are 2004-06, 08-10, 12-14
gen acq_legacy = (ind12_14acq_legacy > 0) | (ind08_11acq_legacy > 0) | (ind04_06acq_legacy > 0)
gen target = (ind12_14target > 0) | (ind08_11target > 0) | (ind04_06target> 0)
gen target2 = (ind12_14target2 > 0) | (ind08_11target2 > 0) | (ind04_06target2 > 0)
gen acq_other = (ind12_14acq_other > 0) | (ind08_11acq_other > 0) | (ind04_06acq_other > 0)
keep if inlist(year, 2006, 2010, 2014)

*  restrict the sample: Legacy, Target, Other forprofits 
keep if forprofit == 1 
drop if target2 == 1 | acq_other == 1 

label var ind08_11acq_legacy "Acquirer * 08-11"
label var ind12_14target "Target * 12-14"

* define post-treatment 
gen postm = ( year >= 2008)
gen postm_target = postm * target
gen postm_legacy = postm * acq_legacy 
label var postm_target "Post 2008 * Target"
label var postm_legacy "Post 2008 * Acquirer"



* check
assert target2 + acq_other == 0 

* run DD as before but with hospital-measure FEs, yeargroup-measure FEs 
* gen hospital-measure FEs
egen hospmeasure = group(id cohort)

*gen year-measure FEs
egen yrmeasure = group(year cohort)

* gen HRR-yeargroup-measure FEs
egen hrryrmeasure = group(hrrcode year cohort)

* gen HRR year time TREND by cohort 
gen hrr_time_ami = hrrcode * year * (cohort == "ami")
gen hrr_time_chf = hrrcode * year * (cohort == "chf")
gen hrr_time_hip = hrrcode * year * (cohort == "hip")
gen hrr_time_pnu = hrrcode * year * (cohort == "pnu")
gen hrr_time_stk = hrrcode * year * (cohort == "stk")

* interact log(beds) with measure
tab cohort, gen(cr)
foreach v of varlist cr* {
	gen lbds`v' = log(beds_tot) * `v'
	local lab: variable label `v'
	local lab = subinstr("`lab'", "cohort==", "log(beds) * ", 1)
	label var lbds`v' "`lab'"
}
egen id2 = group(id)

foreach v in shdx rhdx {


	************************************************************************
	* survival and readmission regression - main
	************************************************************************
	reghdfe `v' postm_legacy postm_target if forprofit == 1, absorb(hospmeasure yrmeasure) vce(cluster id2)

	regsave using "`fpath_output'/`v'_main_20230606", p autoid addlabel(rhs, "`v'", spec, "main") replace 

	
}

	
























